﻿using EmitMapper;
using EmitMapper.MappingConfiguration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SkyNote.Infrastructure.Helper
{
    public class ValueObjectCopyHelper<TFrom, TTo>
    {
        private static ObjectsMapper<TFrom, TTo> mapper;
        static ValueObjectCopyHelper()
        {
            ValueObjectCopyHelper<TFrom, TTo>.mapper = ObjectMapperManager.DefaultInstance.GetMapper<TFrom, TTo>(new DefaultMapConfig().ConvertUsing<System.Guid, string>((System.Guid value) => (value != System.Guid.Empty) ? value.ToString() : string.Empty).ConvertUsing<string, System.Guid>((string value) => string.IsNullOrEmpty(value) ? System.Guid.NewGuid() : new System.Guid(value)));
        }
        public static void CopyObject(TFrom from, TTo to)
        {
            ValueObjectCopyHelper<TFrom, TTo>.mapper.Map(from, to);
        }
        public static TTo CopyObject(TFrom from)
        {
            return ValueObjectCopyHelper<TFrom, TTo>.mapper.Map(from);
        }
    }
}
